<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button @click="addVisible = true" type="primary" size="mini"
          >添加分类</el-button
        >
      </div>
      <el-table stripe border :data="cateList" style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              @click="editOpenHandler(scope.row)"
              size="mini"
              type="primary"
              >修改</el-button
            >
            <el-button @click="remove(scope.row.id)" size="mini" type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--  :befor-close="handleClose" : 关闭前的时间,要传一个函数 -->
    <!-- closed: 关闭动画执行后才触发这个事件,更推荐使用这个事件 -->
    <!-- 如果事件处理函数简写，不能加this -->
    <!-- 添加分类对话框 -->
    <el-dialog
      @closed="$refs.addForm.resetFields()"
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
    >
      <el-form :model="addForm" :rules="addRules" ref="addForm">
        <el-form-item
          label="分类名称"
          :label-width="formLabelWidth"
          prop="cate_name"
        >
          <el-input v-model="addForm.cate_name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="分类别名"
          :label-width="formLabelWidth"
          prop="cate_alias"
        >
          <el-input v-model="addForm.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button @click="addHandler" type="primary">添加</el-button>
      </div>
    </el-dialog>
    <!-- 添加分类对话框 -->

    <!-- 修改分类对话框 -->
    <el-dialog
      @closed="$refs.editForm.resetFields()"
      title="修改文章分类"
      :visible.sync="editVisable"
      width="35%"
    >
      <el-form :model="editForm" :rules="addRules" ref="editForm">
        <el-form-item
          label="分类名称"
          :label-width="formLabelWidth"
          prop="cate_name"
        >
          <el-input v-model="editForm.cate_name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="分类别名"
          :label-width="formLabelWidth"
          prop="cate_alias"
        >
          <el-input v-model="editForm.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisable = false">取 消</el-button>
        <el-button @click="editHandler" type="primary">确定</el-button>
      </div>
    </el-dialog>
    <!-- 修改分类对话框 -->
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      },
      editVisable: false,
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      formLabelWidth: '80px'
    }
  },
  created () {
    this.getArtCate()
  },
  methods: {
    async getArtCate () {
      const { data: res } = await this.$axios.get('/my/cate/list')
      if (res.code === 0) {
        // console.log(res.data)
        this.cateList = res.data
        // console.log(this.cateList)
      }
    },
    addHandler () {
      this.$refs.addForm.validate(async (result) => {
        if (!result) return
        const { data: res } = await this.$axios.post(
          '/my/cate/add',
          this.addForm
        )
        if (res.code === 0) {
          this.addVisible = false
          this.getArtCate()
        }
      })
    },
    editHandler () {
      this.$refs.editForm.validate(async (result) => {
        if (!result) return
        const { data: res } = await this.$axios.put(
          '/my/cate/info',
          this.editForm
        )
        if (res.code === 0) {
          this.editVisable = false
          this.getArtCate()
        }
      })
    },
    editOpenHandler (row) {
      if (row.id === 1 || row.id === 2) {
        this.$message.error('亲,不能允许删除这条数据哦😂!')
      } else {
        this.editVisable = true
        this.editForm = { ...row }
      }
      // console.log(row)
    },
    async remove (id) {
      // console.log(id)
      /* .then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      }) */
      if (id === 1 || id === 2) {
        this.$message.error('该条数据已被禁止删除!')
      } else {
        const res = await this.$confirm('您确定需要删除该条数据吗?', '提示', {
          // confirmButtonText: '确定',
          // cancelButtonText: '取消',
          type: 'warning'
        }).catch((e) => this.$message.error('回头是岸!'))
        // console.log(res)
        if (res === 'confirm') {
          // 点击确认是confirm,点击取消是cancel
          // 1.发送请求删除数据
          // 第二个参数直接写数据对象表达式传入data请求体
          // 如果需要查询字符串,需要手动指定params
          const { data: res } = await this.$axios.delete('/my/cate/del', {
            params: { id }
          })
          if (res.code === 0) this.getArtCate()
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
